<?php

/*
 * @copyright :     Ienter asia Technologies
 * @author :        Quyet
 * @version         v.1.0
 * @created         18/6/2014
 * @Description :   Model Employee 
 */
if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Mmod_shiftinfo extends MYAD_Model {

    protected $table_name = 'shiftinfo';

    function __construct() {
        parent::__construct();
    }

    /*
     * @author :        Quyet
     * @created:        2015/8/25
     * @Description:    Get all data for index shift info
     * @Input :         (Integer) $page
     * @Output :        (Array) data for view data
     */

    function getAllData($page = 0) {
        /* initialiazion condition */
        $condition = array(
            'where' => array('CompanyCode' => $this->session->userdata('CompanyCode')),
        );
        /* load all label */
        $labelData = $this->getLabelData();

        /* load all employee data range */
        $allData = $this->countResult($this->table_name, array('CompanyCode' => $this->session->userdata('CompanyCode')));

        /* load pagination */
        $pagiData = array(
            'base_url' => base_url('shift-info'),
            'total_rows' => $allData,
        );
        $pagination = $this->config->item('pagination');
        $offset = isset($pagiData['per_page']) ? $pagiData['per_page'] : $pagination['per_page'];
        $pagi = $this->getPagination($pagiData);

        /* load position data range */
        $condition['limit'] = array($page, $offset);
        $condition['order_by'] = 'ShiftId ASC';
        $positionData = $this->getShipInfo($condition);


        /* load config */
        $data = array(
            'show' => 'index',
            'pagi' => $pagi,
            'message' => $this->lang->line('emp_sli_table_empty_data'),
            'order' => $page,
            'base_flag' => TRUE,
        );
        return array_merge($data, $labelData, $positionData);
    }

    /*
     * @author :        Quyet
     * @created:        0214/8/25
     * @Description:    Get label data of shift info
     * @Input :         void
     * @Output :        (Array) data label
     */

    function getLabelData() {
        $title = $this->lang->line('Sif_lst_title');
        $lblNewRegister = $this->lang->line('Sif_lst_lblNewRegister');
        $lblOrder = $this->lang->line('Sif_lst_table_col_order');
        $lblShiftName = $this->lang->line('Sif_lst_table_col_shift_name');
        $lblStarttime = $this->lang->line('Sif_Starttime');
        $lblEndTime = $this->lang->line('Sif_Endtime');
        $lblAction = $this->lang->line('po_table_col_Action');
        $lblActive = $this->lang->line('po_table_col_Active');

        $data = array(
            'title' => (empty($title) ? 'Shift Infomation List' : $title),
            'lblNewRegister' => (empty($lblNewRegister) ? 'hift Infomation List' : $lblNewRegister),
            'lblOrder' => (empty($lblOrder) ? 'Order' : $lblOrder),
            'lblShitfName' => (empty($lblShiftName) ? 'Shift Name' : $lblShiftName),
            'lblStarttime' => (empty($lblStarttime) ? 'Start Time' : $lblStarttime),
            'lblEndTime' => (empty($lblEndTime) ? 'End Time' : $lblEndTime),
            'lblAction' => (empty($lblAction) ? 'Action' : $lblAction),
            'lblActive' => (empty($lblActive) ? 'Active' : $lblActive),
        );
        return $data;
    }

    /*
     * @author :        Quyet
     * @created:        30/6/2014
     * @Description:    check validate form shipinfo
     * @Input :         void
     * @Output :        boolean
     */

    function validate() {
        /* convert */
        $_POST['txtTotalWorkHours'] = mb_convert_kana($_POST['txtTotalWorkHours'], 'a');
        $_POST['txtStartTime'] = mb_convert_kana($_POST['txtStartTime'], 'a');
        $_POST['txtEndTime'] = mb_convert_kana($_POST['txtEndTime'], 'a');
        $_POST['txtStartRestTime1'] = mb_convert_kana($_POST['txtStartRestTime1'], 'a');
        $_POST['txtEndRestTime1'] = mb_convert_kana($_POST['txtEndRestTime1'], 'a');
        $_POST['txtRestTime2'] = mb_convert_kana($_POST['txtRestTime2'], 'a');
        $_POST['txtEndEveningOvertime'] = mb_convert_kana($_POST['txtEndEveningOvertime'], 'a');
        $_POST['txtRestTime3'] = mb_convert_kana($_POST['txtRestTime3'], 'a');
        $_POST['txtRoundedTimeBlock'] = mb_convert_kana($_POST['txtRoundedTimeBlock'], 'a');

        $this->form_validation->set_error_delimiters('', '');
        $this->form_validation->set_rules('txtShiftId', 'txtShiftId', 'is_natural');
        $this->form_validation->set_rules('txtShiftName', $this->lang->line('Sif_ShiftName'), 'required|max_length[255]');
        $this->form_validation->set_rules('txtTotalWorkHours', $this->lang->line('Sif_TotalWorkHours'), 'required|greater_than[3]|less_than[13]|callback_validate_totalWorkHour');
        $this->form_validation->set_rules('txtStartTime', $this->lang->line('Sif_Starttime'), 'required|callback_validate_time');
        $this->form_validation->set_rules('txtEndTime', $this->lang->line('Sif_Endtime'), 'required|callback_validate_time|callback_lessThanDate[txtEndRestTime1]');
        $this->form_validation->set_rules('txtStartRestTime1', $this->lang->line('Sif_StartRestime1'), 'required|callback_validate_time|callback_lessThanDate[txtStartTime]');
        $this->form_validation->set_rules('txtEndRestTime1', $this->lang->line('Sif_EndRestime1'), 'required|callback_validate_time|callback_lessThanDate[txtStartRestTime1]');
        $this->form_validation->set_rules('txtRestTime2', $this->lang->line('Sif_Restime2'), 'required|integer|is_natural_no_zero|callback_validate_restime2');
        $this->form_validation->set_rules('txtEndEveningOvertime', $this->lang->line('Sif_EndEveningOvertime'), 'required|callback_validate_time|callback_lessThanDate[txtEndTime]');
        $this->form_validation->set_rules('txtRestTime3', $this->lang->line('Sif_Restime3'), 'required|integer|is_natural_no_zero|callback_validate_restime3');
        $this->form_validation->set_rules('txtRoundedTimeBlock', $this->lang->line('Sif_RoundedTimeBlock'), 'required|is_natural_no_zero');
        $this->form_validation->set_rules('txtDelete', $this->lang->line('po_update_lblDelete'), 'max_length[1]');

        return $this->form_validation->run();
    }

    /*
     * @author :        Quyet
     * @created:        30/6/2014
     * @Description:    update shipinfo
     * @Input :         (integer) $shiftId
     * @Output :        (Array[boolean/ message])
     */

    function updateShipInfo($shiftId = 0) {
        $conditionDuplicate = array(
            'ShiftId !=' => $shiftId,
            'CompanyCode' => $this->session->userdata('CompanyCode'),
            'ShiftName' => $this->input->post('txtShiftName'),
            'Del_Flag' => FALSE,
        );
        $duplicate = $this->countResult($this->table_name, $conditionDuplicate);
        if ($duplicate) {
            return array(
                'flag' => FALSE,
                'message' => $this->lang->line('Sif_ShiftName') . ': ' . $this->lang->line('emp_updateDB_duplicate')
            );
        }
        $data = array(
            'CompanyCode' => $this->session->userdata('CompanyCode'),
            'ShiftName' => $this->input->post('txtShiftName'),
            'TotalWorkHours' => (float) $this->input->post('txtTotalWorkHours'),
            'StartTime' => $this->input->post('txtStartTime'),
            'EndTime' => $this->input->post('txtEndTime'),
            'StartRestTime1' => $this->input->post('txtStartRestTime1'),
            'EndRestTime1' => $this->input->post('txtEndRestTime1'),
            'RestTime2' => $this->input->post('txtRestTime2'),
            'EndEveningOvertime' => $this->input->post('txtEndEveningOvertime'),
            'RestTime3' => $this->input->post('txtRestTime3'),
            'RoundedTimeBlock' => $this->input->post('txtRoundedTimeBlock'),
            'Del_Flag' => $this->input->post('txtDelete'),
        );
        if ($shiftId) {
            $condition['ShiftId'] = $shiftId;
        }
        $temp = $this->updateDB($this->table_name, $data, $condition);
        if ($temp) {
            return array(
                'flag' => TRUE,
                'message' => $this->lang->line('emp_updateDB_success')
            );
        } else {
            return array(
                'flag' => FALSE,
                'message' => $this->lang->line('emp_updateDB_fall')
            );
        }
    }

    /*
     * @author :        Quyet
     * @created:        30/6/2014
     * @Description:    basic data for new shipinfo
     * @Input :         (Interger) $shiftId
     * @Output :        Array
     */

    function getAddShipInfo($shiftId = 0) {
        /* get language info */
        $title = ($shiftId) ? $this->lang->line('Sif_update_title') : $this->lang->line('Sif_title');
        $dialogTitle = $this->lang->line('emp_dialog_title');
        $lblBack = $this->lang->line('holi_update_lblBack');
        $lblShiftName = $this->lang->line('Sif_ShiftName');
        $lblTotalWorkHours = $this->lang->line('Sif_TotalWorkHours');
        $lblStarttime = $this->lang->line('Sif_Starttime');
        $lblEndtime = $this->lang->line('Sif_Endtime');
        $lblStartRestime1 = $this->lang->line('Sif_StartRestime1');
        $lblEndRestime1 = $this->lang->line('Sif_EndRestime1');
        $lblRestime2 = $this->lang->line('Sif_Restime2');
        $lblEndEveningOvertime = $this->lang->line('Sif_EndEveningOvertime');
        $lblRestime3 = $this->lang->line('Sif_Restime3');
        $lblRoundedTimeBlock = $this->lang->line('Sif_RoundedTimeBlock');
        $lblDelete = $this->lang->line('po_update_lblDelete');
        $lblSubmit = $this->lang->line('Sif_Submit');

        /* get shipinfo */
        $shipInfo = $this->getShipInfoByID($shiftId);
        if (!isset($_POST['btnSubmit']) && $shipInfo) {

            /* is load default */
            $emp = array(
                'txtShiftId' => $shipInfo->ShiftId,
                'txtShiftName' => dQuotes($shipInfo->ShiftName),
                'txtTotalWorkHours' => $shipInfo->TotalWorkHours,
                'txtStartTime' => setTime($shipInfo->StartTime),
                'txtEndTime' => setTime($shipInfo->EndTime),
                'txtStartRestTime1' => setTime($shipInfo->StartRestTime1),
                'txtEndRestTime1' => setTime($shipInfo->EndRestTime1),
                'txtRestTime2' => $shipInfo->RestTime2,
                'txtEndEveningOvertime' => setTime($shipInfo->EndEveningOvertime),
                'txtRestTime3' => $shipInfo->RestTime3,
                'txtRoundedTimeBlock' => $shipInfo->RoundedTimeBlock,
                'txtDelete' => $shipInfo->Del_Flag,
            );
        } else {
            $emp = array(
                'txtShiftId' => set_value('txtShiftId'),
                'txtShiftName' => set_value('txtShiftName'),
                'txtTotalWorkHours' => set_value('txtTotalWorkHours'),
                'txtStartTime' => set_value('txtStartTime'),
                'txtEndTime' => set_value('txtEndTime'),
                'txtStartRestTime1' => set_value('txtStartRestTime1'),
                'txtEndRestTime1' => set_value('txtEndRestTime1'),
                'txtRestTime2' => set_value('txtRestTime2'),
                'txtEndEveningOvertime' => set_value('txtEndEveningOvertime'),
                'txtRestTime3' => set_value('txtRestTime3'),
                'txtRoundedTimeBlock' => set_value('txtRoundedTimeBlock'),
                'txtDelete' => set_value('txtDelete'),
            );
        }

        $temp = array(
            'js' => array('jquery-ui.js'),
            'css' => array('jquery-ui.css'),
            'show' => 'update',
            /* set label */
            'title' => (empty($title) ? 'Basic Shift Infomation' : $title),
            'lblBack' => (empty($lblBack) ? 'Back' : $lblBack),
            'dialogTitle' => (empty($dialogTitle) ? 'Notification' : $dialogTitle),
            'lblShiftName' => (empty($lblShiftName) ? 'Shift Name' : $lblShiftName),
            'lblTotalWorkHours' => (empty($lblTotalWorkHours) ? 'Total Work Hours' : $lblTotalWorkHours),
            'lblStarttime' => (empty($lblStarttime) ? 'Start time' : $lblStarttime),
            'lblEndtime' => (empty($lblEndtime) ? 'End time' : $lblEndtime),
            'lblStartRestTime1' => (empty($lblStartRestime1) ? 'Start Restime 1' : $lblStartRestime1),
            'lblEndRestTime1' => (empty($lblEndRestime1) ? 'End Restime 1' : $lblEndRestime1),
            'lblRestime2' => (empty($lblRestime2) ? 'Restime 2' : $lblRestime2),
            'lblEndEveningOvertime' => (empty($lblEndEveningOvertime) ? 'End Evening Overtime' : $lblEndEveningOvertime),
            'lblRestime3' => (empty($lblRestime3) ? 'lblRestime 3' : $lblRestime3),
            'lblRoundedTimeBlock' => (empty($lblRoundedTimeBlock) ? 'Rounded Time Block' : $lblRoundedTimeBlock),
            'lblDelete' => (empty($lblDelete) ? 'Deleted' : $lblDelete),
            'lblSubmit' => (empty($lblSubmit) ? 'Submit' : $lblSubmit),
                /* set data */
        );
        return array_merge($temp, $emp);
    }

    /*
     * @author :        Quyet
     * @created:        2014/8/25
     * @Description:    get getShipInfo by condition
     * @Input :         (array) $condition
     * @Output :        (Array)
     */

    function getShipInfo($condition = array()) {
        $temp = $this->selectDB($this->table_name, $condition);
        return array('data' => $temp);
    }

    /*
     * @author :        Quyet
     * @created:        30/6/2014
     * @Description:    get shipinfo
     * @Input :         void
     * @Output :        (object)/false
     */

    function getShipInfoByID($shipID = 0) {
        $condition = array(
            'where' => array(
                'ShiftId' => (integer) $shipID,
                'CompanyCode' => $this->session->userdata('CompanyCode')
            ),
            'disable_del' => TRUE,
            'first' => TRUE
        );
        return $this->selectDB($this->table_name, $condition);
    }

    /*
     * @author :        Quyet
     * @created:        30/6/2014
     * @Description:    check validation id of shipinfo;
     * @Input :         (integer) $shiftId
     * @Output :        integer
     */

    function countShipInfoByID($shiftId = 0) {
        $condition = array(
            'ShiftId' => $shiftId,
            'CompanyCode' => $this->session->userdata('CompanyCode')
        );
        return $this->countResult($this->table_name, $condition);
    }

}

?>